package com.romeo_foxtrot.labs.vaadin;

import java.io.Serializable;

import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.Application;
import com.vaadin.ui.Component;
import com.vaadin.ui.Window;

public abstract class AppRunner<A extends Application> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5449160919434646696L;

	protected final A app;

	private final EventBus eventBus;

	public AppRunner(A app) {
		this.app = app;
		eventBus = new EventBus(this);
	}

	/**
	 * 在当前窗口显示提示信息
	 * 
	 * @param view
	 *            必须已经添加到画面中
	 * @param caption
	 * @param description
	 * @param type
	 */
	public static void showNotification(Component view, String caption,
			String description, int type) {

		Window.Notification n = new Window.Notification(caption, description,
				type);

		// /*
		// * auto hide after 30"
		// */
		// n.setDelayMsec(30000);

		view.getWindow().showNotification(n);

		return;
	}

	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	public <H extends EventHandler> HandlerRegistration addHandler(
			GwtEvent.Type<H> type, H handler) {
		return eventBus.addHandler(type, handler);
	}

	public A getApp() {
		return app;
	}
}
